package com.linkage.educloud.ah.view.activity.app;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.EduAppAdapter;
import com.linkage.educloud.ah.adapter.EduResAdapter;
import com.linkage.educloud.ah.bean.EduAppBean;
import com.linkage.educloud.ah.bean.EduResBean;
import com.linkage.educloud.ah.mvp.contract.ISearchAppContract;
import com.linkage.educloud.ah.mvp.presenter.SearchAppPresenter;
import com.linkage.educloud.ah.view.BasicActivity;
import com.linkage.educloud.ah.widget.Flowlayout;
import com.linkage.educloud.ah.widget.MyMore;
import com.zzhou.common.library.util.StringUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Mr'Tang on 2018/12/20.
 */

public class SearchAppActivity extends BasicActivity implements ISearchAppContract.IView, View.OnClickListener {

    @BindView(R.id.common_back)
    ImageView common_back;
    @BindView(R.id.common_edittext)
    EditText common_edittext;
    @BindView(R.id.common_search)
    TextView common_search;
    @BindView(R.id.ac_searchapp_mymore_histroy)
    MyMore ac_searchapp_mymore_histroy;
    @BindView(R.id.ac_searchapp_flow)
    Flowlayout ac_searchapp_flow;
    @BindView(R.id.ac_searchapp_lin_res)
    LinearLayout ac_searchapp_lin_res;
    @BindView(R.id.ac_searchapp_title_res)
    MyMore ac_searchapp_title_res;
    @BindView(R.id.ac_searchapp_list_res)
    XRecyclerView ac_searchapp_list_res;
    @BindView(R.id.ac_searchapp_mymore_res)
    MyMore ac_searchapp_mymore_res;
    @BindView(R.id.ac_searchapp_lin_app)
    LinearLayout ac_searchapp_lin_app;
    @BindView(R.id.ac_searchapp_title_app)
    MyMore ac_searchapp_title_app;
    @BindView(R.id.ac_searchapp_list_app)
    XRecyclerView ac_searchapp_list_app;
    @BindView(R.id.ac_searchapp_mymore_app)
    MyMore ac_searchapp_mymore_app;

    ISearchAppContract.IPresenter iPresenter;

    EduResAdapter resAdapter;
    List<EduResBean> resList = new ArrayList<>();
    EduAppAdapter appAdapter;
    List<EduAppBean> appList = new ArrayList<>();

    private static final String KEY_LABEL = "label";
    private static final String KEY_LABEL_STRING = "label_string";
    private String label = "";
    private int is_add = 0;//0加入label 否则则是从历史点进去的不保存


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_search_app;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iPresenter = new SearchAppPresenter(this, this);
        common_back.setOnClickListener(this);
        common_search.setOnClickListener(this);
        ac_searchapp_mymore_res.setOnClickListener(this);
        ac_searchapp_mymore_app.setOnClickListener(this);

        initFlow();
        initRecylerview();
    }

    private void initFlow() {
        label = getSharedPreferences(KEY_LABEL, MODE_PRIVATE).getString(KEY_LABEL_STRING, "");
        if (label.isEmpty()) {
            ac_searchapp_mymore_histroy.setVisibility(View.GONE);
            ac_searchapp_flow.setVisibility(View.GONE);
        } else {
            ac_searchapp_mymore_histroy.setVisibility(View.VISIBLE);
            ac_searchapp_flow.setVisibility(View.VISIBLE);
            String[] strs = label.split("%2B");
            final List<String> myHistory = Arrays.asList(strs);
            ac_searchapp_flow.removeAllViews();
            for (int a = 0; a < myHistory.size(); a++) {
                TextView myText = new TextView(this);
                myText.setTextSize(12);
                myText.setTextColor(Color.parseColor("#000000"));
                myText.setText(StringUtil.cuteString(myHistory.get(a), 5));
                myText.setBackgroundResource(R.drawable.flow_histroy_bg);
                ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                lp.setMargins(10, 10, 10, 10);
                ac_searchapp_flow.addView(myText, lp);

                myText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        is_add = 1;
                        int position = (Integer) v.getTag();
                        showZloading();
                        iPresenter.searchResouces(myHistory.get(position));
                    }
                });
                myText.setTag(a);
            }
        }
    }

    public void initDate(List<EduResBean> reslist, List<EduAppBean> applist) {
        if (is_add == 0) {
            label = label.isEmpty() ? common_edittext.getText().toString().trim() : label + "%2B" + common_edittext.getText().toString().trim();
            getSharedPreferences(KEY_LABEL, MODE_PRIVATE).edit().putString(KEY_LABEL_STRING, label).apply();
        }
        label = label.isEmpty() ? common_edittext.getText().toString().trim() : label + "%2B" + common_edittext.getText().toString().trim();
        getSharedPreferences(KEY_LABEL, MODE_PRIVATE).edit().putString(KEY_LABEL_STRING, label).apply();
        hideZloading();
        common_edittext.setText("");
        if (resList.size() > 0) {
            ac_searchapp_lin_res.setVisibility(View.VISIBLE);
            if (resList.size() > 3) {
                reslist = reslist.subList(0, 2);
                ac_searchapp_mymore_res.setVisibility(View.VISIBLE);
            }
            resAdapter.addAll(reslist);
        }

        if (appList.size() > 0) {
            ac_searchapp_lin_app.setVisibility(View.VISIBLE);
            if (appList.size() > 3) {
                applist = applist.subList(0, 2);
                ac_searchapp_mymore_app.setVisibility(View.VISIBLE);
            }
            appAdapter.addAll(applist);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                finish();
                break;
            case R.id.common_search:
                showZloading();
                iPresenter.searchResouces(common_edittext.getText().toString().trim());
                break;
            case R.id.ac_searchapp_mymore_res:
                EduResListActivity.launch(this, 1, common_edittext.getText().toString().trim());
                break;
            case R.id.ac_searchapp_mymore_app:
                EduAppListActivity.launch(this, "全部应用", 2, -1, common_edittext.getText().toString().trim());
                break;
        }
    }

    private void initRecylerview() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        ac_searchapp_list_res.setLayoutManager(layoutManager);
        ac_searchapp_list_res.setItemAnimator(new DefaultItemAnimator());
        ac_searchapp_list_res.setLoadingMoreEnabled(false);
        ac_searchapp_list_res.setPullRefreshEnabled(false);
        resAdapter = new EduResAdapter(this, resList);
        ac_searchapp_list_res.setAdapter(resAdapter);

        ac_searchapp_list_app.setLayoutManager(layoutManager);
        ac_searchapp_list_app.setItemAnimator(new DefaultItemAnimator());
        ac_searchapp_list_app.setLoadingMoreEnabled(false);
        ac_searchapp_list_app.setPullRefreshEnabled(false);
        appAdapter = new EduAppAdapter(this, appList);
        ac_searchapp_list_app.setAdapter(appAdapter);
    }
}
